package ru.urfc.portal.ui.client.googlets.client;

import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.Style;
import ru.urfc.portal.ui.client.controllers.ItemsController;
import ru.urfc.portal.ui.client.controllers.SimpleItemsController;
import ru.urfc.portal.ui.client.controllers.events.PostEvent;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;
import ru.urfc.portal.ui.client.validators.GxtAdapter;

import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Widget;

public class AssociatedUserCreate extends BaseGooglet {

	interface Binder extends UiBinder<Widget, AssociatedUserCreate> {}
	private static Binder binder = GWT.create(Binder.class);
	
	@UiField Style style;
//	@UiField TextField<String> firstName;
//	@UiField TextField<String> middleName;
//	@UiField TextField<String> lastName;
	@UiField TextField<String> email;
	
	public Type<ItemsController> SUBMIT = new Type<ItemsController>();

	@UiHandler("submit")
	void submit(ClickEvent e) {
		getEventsDispatcher().fireEvent(new PostEvent(SUBMIT));
	}

	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
		email.setAllowBlank(false);
		email.setValidator(GxtAdapter.EMAIL_VALIDATOR);
		
		GridDescriptor descriptor = new GridDescriptor(USER, CLIENTS, getClientId(), CLIENT_ASSOCIATED_USERS);
//		descriptor.addTextField("firstName", firstName);
//		descriptor.addTextField("middleName", middleName);
//		descriptor.addTextField("lastName", lastName);
		descriptor.addTextField("email", email);
		
		register(SUBMIT, new SimpleItemsController(descriptor) {
			@Override
			public void onUpdate(Object data) {
				if (data.equals(HTTP_NO_CONTENT)) {
					redirect(PAGE_ASSOCIATED_USERS);
				}
			}			
		});
	}

}